<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');

class Profile extends SMB_Controller {
	
	public function __construct() {
		parent::__construct();
		$this->load->library('sponsorship');
	}
	
	public function index() {
		$this->_get_userinfo();
		
		$this->_load_common_header();
		$this->_load_common_navigation();
		$this->_load_common_sidebar(get_user_sidebar($this->userinfo['role'], 'profile'));
		$params = array(
			'sponsor' => $this->sponsorship->get_sponsor_info($this->userinfo['username']),
			'editstatus' => $this->session->flashdata('editstatus')
		);
		$this->load->view('sponsor/profile', $params);
		$this->_load_common_footer();
	}
	
	public function changepassword() {
		$this->_get_userinfo();
		
		$this->_load_common_header();
		$this->_load_common_navigation();
		$this->_load_common_sidebar(get_user_sidebar($this->userinfo['role'], 'profile'));
		
		$this->form_validation->set_error_delimiters('<span class="error">', '</span>');
		if ($this->form_validation->run('change_password') == FALSE) {
			$params = array(
				'changestatus' => '',
				'username' => $this->userinfo['username']
			);
		} else {
			$username = $this->input->post('username', TRUe);
			$old = $this->input->post('old', TRUE);
			$new = $this->input->post('new', TRUE);
			$result = $this->sponsorship->change_password($username, $old, $new);
			if ($result == FALSE) {
				$params = array(
					'changestatus' => 'failed',
					'username' => $this->userinfo['username']
				);
			} else {
				$params = array(
					'changestatus' => 'success',
					'username' => $this->userinfo['username']
				);
			}
		}
		
		$this->load->view('sponsor/profile_changepassword', $params);
		$this->_load_common_footer();
	}
	
	public function _check_password($str) {
		$result = $this->authentication->login($this->userinfo['username'], $str);
		if ($result == FALSE) {
			$this->form_validation->set_message('_check_password', 'Password lama tidak sesuai.');
			return FALSE;
		} else {
			return TRUE;
		}
	}
	
	public function edit() {
		$this->_get_userinfo();
		
		$this->form_validation->set_error_delimiters('<span class="error">', '</span>');
		if ($this->form_validation->run('edit_sponsor_profile') == FALSE) {
			$this->_load_common_header();
			$this->_load_common_navigation();
			$this->_load_common_sidebar(get_user_sidebar($this->userinfo['role'], 'profile'));
			$params = array(
				'sponsor' => $this->sponsorship->get_sponsor_info($this->userinfo['username'])
			);
			$this->load->view('sponsor/profile_edit', $params);
			$this->_load_common_footer();
		} else {
			$id= $this->input->post('id', TRUE);
			$name = $this->input->post('name', TRUE);
			$address = $this->input->post('address', TRUE);
			$contactName = $this->input->post('contactName', TRUE);
			$contactNo = $this->input->post('contactNo', TRUE);
			$result = $this->sponsorship->edit_sponsor_info($id, $name, $address, $contactName, $contactNo);
			if ($result == FALSE) {
				$this->_load_common_header();
				$this->_load_common_navigation();
				$this->_load_common_sidebar(get_user_sidebar($this->userinfo['role'], 'profile'));
				$params = array(
					'id' => $id,
					'name' => $name,
					'address' => $address,
					'contactName' => $contactName,
					'contactNo' => $contactNo
				);
				$this->load->view('sponsor/profile_edit', $params);
				$this->_load_common_footer();
			} else {
				$this->session->set_flashdata('editstatus', 'success');
				redirect('sponsor/profile', 'refresh');
			}
		}
	}
}